// ==========================================================================
// MainPage.cs
// Silverlight Shapes Library
// ==========================================================================
// Copyright (c) Silverlight Shapes Development Group
// All rights reserved.
// ==========================================================================

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using SD.Samples.Messages;
using SD.Utils.Windows;

namespace SD.Samples
{
    /// <summary>
    /// Code behind for MainPage.xaml.
    /// </summary>
    public partial class MainPage : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainPage"/> class.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            Messenger.Default.Register<NavigateToSampleMessage>(this, OnNavigateToSample);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Called when the <see cref="NavigateToSampleMessage"/> has been send.
        /// </summary>
        /// <param name="message">The <see cref="NavigateToSampleMessage"/> instance that contains all data.</param>
        public void OnNavigateToSample(NavigateToSampleMessage message)
        {
            string path = string.Format(CultureInfo.CurrentCulture, "Samples/{0}", message.Content);

            ContentFrame.Navigate(new Uri(path, UriKind.Relative));
        }

        /// <summary>
        /// Handles the Navigated event of the ContentFrame control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Navigation.NavigationEventArgs"/> instance containing the event data.</param>
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hyperlinkButton = child as HyperlinkButton;

                if (hyperlinkButton != null && hyperlinkButton.NavigateUri != null)
                {
                    if (ContentFrame.UriMapper.MapUri(e.Uri).ToString().Equals(ContentFrame.UriMapper.MapUri(hyperlinkButton.NavigateUri).ToString()))
                    {
                        VisualStateManager.GoToState(hyperlinkButton, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hyperlinkButton, "InactiveLink", true);
                    }
                }
            }
        }

        /// <summary>
        /// Handles the NavigationFailed event of the ContentFrame control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Navigation.NavigationFailedEventArgs"/> instance containing the event data.</param>
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            string errorMessage = string.Format(CultureInfo.CurrentCulture, "Path {0} does not exist.", e.Uri);

            ErrorWindow.Show(errorMessage);
            e.Handled = true;
        }

        #endregion
    }
}